cmake_minimum_required(VERSION 3.0)

project(adv LANGUAGES Fortran)

if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
	set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-none -ffpe-trap=invalid,zero,overflow")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O0 -fcheck=all -g -fbacktrace")
  else ()
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Ofast")
  endif ()
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-wrap-margin -O0 -check bounds -g -traceback -qmkl=sequential -fp-model precise")
  else ()
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-wrap-margin -O3 -align array64byte -qmkl=sequential")
  endif ()
endif ()

if (DEFINED ENV{NETCDF} AND (NOT DEFINED ENV{NETCDF_ROOT}))
  set(ENV{NETCDF_ROOT} $ENV{NETCDF})
endif ()
if (DEFINED ENV{NETCDF_ROOT})
  include_directories("$ENV{NETCDF_ROOT}/include")
  link_directories("$ENV{NETCDF_ROOT}/lib")
else ()
  find_package(PkgConfig)
  if (PKG_CONFIG_FOUND)
    pkg_search_module(NETCDF REQUIRED netcdf)
    if (NETCDF_FOUND)
      include_directories(${NETCDF_INCLUDEDIR})
      link_directories(${NETCDF_LIBDIR})
    else ()
      message(FATAL_ERROR "Pkg-config could not find netcdf library!")
    endif ()
  else ()
    message(FATAL_ERROR "Unable to find pkg-config library!")
  endif ()
endif ()
set(EXTERNAL_LIBS netcdff netcdf)

set(adv_files
	src/utils/const_mod.F90
	src/utils/namelist_mod.F90
	src/utils/process_mod.F90
	src/utils/allocator_mod.F90
	src/utils/parallel_mod.F90
	src/utils/sphere_geometry_mod.F90
	src/utils/time_mod.F90
  src/utils/history_mod.F90
  src/utils/math_mod.F90
	src/tests/2D/solid_rotation_test_mod.F90
  src/tests/2D/deform_test_mod.F90
  src/tests/2D/stationary_vortex_test_mod.F90
  src/tests/2D/moving_vortex_test_mod.F90
  src/tests/3D/deform_flow_test_mod.F90
  src/tests/3D/hadley_merid_test_mod.F90
  src/tests/3D/thin_cloud_test_mod.F90
	src/core/mesh_mod.F90
  src/core/vert_coord_mod.F90
	src/core/state_mod.F90
	src/core/tracer_mod.F90
	src/core/ffsl_mod.F90
  src/core/upwind_mod.F90
  src/core/step_mod.F90
	src/core/adv_mod.F90
  src/core/operators_mod.F90
	)

add_library(adv ${adv_files})
target_link_libraries(adv ${EXTERNAL_LIBS})

add_executable(adv_driver.exe src/drivers/adv_driver.F90)
target_link_libraries(adv_driver.exe adv)
